﻿#pragma execution_character_set("utf-8")

#include "frmsavelog.h"
#include "ui_frmsavelog.h"
#include "savelog.h"
#include "qdatetime.h"
#include "qtimer.h"
#include "qdebug.h"

frmSaveLog::frmSaveLog(QWidget *parent) : QWidget(parent), ui(new Ui::frmSaveLog)
{
    ui->setupUi(this);
    this->initForm();
}

frmSaveLog::~frmSaveLog()
{
    delete ui;
}

/**
 * @brief frmSaveLog::initForm
 * initForm 初始化框架，以后一些初始化代码可以放到这里去运行
 */
void frmSaveLog::initForm()
{
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(append()));//定时器，每隔1秒中执行一次
    timer->setInterval(1000);

    SaveLog::Instance()->setPath(qApp->applicationDirPath());
}

void frmSaveLog::append()
{
    QString msg = QString("当前时间: %1  测试打印输出消息").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"));
    ui->txtMain->append(msg);
    static int s32Count = 0;
    s32Count++;
    switch (s32Count%5) {
        case 1:
            QDebug(QtSystemMsg) << msg.prepend("QtSystemMsg");
            break;
        case 2:
            QDebug(QtWarningMsg) << msg.prepend("QtWarningMsg");
            break;
        case 3:
            QDebug(QtCriticalMsg) << msg.prepend("QtCriticalMsg");
            break;
//        case 4:
//            QDebug(QtFatalMsg) << msg.prepend("QtFatalMsg");   //这种消息类型一旦打印，程序就停止了执行。
//            break;
        default:
            QDebug(QtDebugMsg) << msg;
            break;
    }
}

void frmSaveLog::on_btnDebug_clicked()
{
    QString msg = QString("当前时间: %1  手动插入消息").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"));
    ui->txtMain->append(msg);
    qDebug() << msg;
}

/**
 * @brief frmSaveLog::on_ckTimer_stateChanged
 * @param arg1  定时器的开始和停止
 */
void frmSaveLog::on_ckTimer_stateChanged(int arg1)
{
    qDebug() << "状态改变" << arg1;
    if (arg1 == 0) {
        timer->stop();
    } else {
        timer->start();
    }
}

void frmSaveLog::on_ckNet_stateChanged(int arg1)
{
    SaveLog::Instance()->setToNet(arg1 != 0);
}

/**
 * @brief frmSaveLog::on_ckSave_stateChanged
 * @param arg1  记录打印信息到文件 的开始或者停止
 */
void frmSaveLog::on_ckSave_stateChanged(int arg1)
{
    qDebug() << "保存状态" << arg1;
    if (arg1 == 0) {
        SaveLog::Instance()->stop();
    } else {
        SaveLog::Instance()->start();
    }
}
